import 'dart:convert';

import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:iidx_progress_tool/components/musiclistcomponent/level_dropdown_button.dart';
import 'package:iidx_progress_tool/components/musiclistcomponent/list_content.dart';
import 'package:iidx_progress_tool/components/musiclistcomponent/version_dropdown_button.dart';
import 'package:iidx_progress_tool/data/model/music_data.dart';

import 'musiclistcomponent/filter_model.dart';

class MusicList extends StatefulWidget {
  const MusicList({super.key});

  @override
  State<StatefulWidget> createState() => _MusicListState();
}

class _MusicListState extends State<MusicList> {
  var musicDataList;
  FilterModel filter = FilterModel();

  _MusicListState() {
    _init();

    filter.addListener(() {
      setState(() {});
    });
  }

  @override
  Widget build(BuildContext context) {
    if (musicDataList == null) {
      return const Center(
        child: Text(
          '数据加载中',
          style: TextStyle(fontSize: 20),
        ),
      );
    }
    return _buildMusicInfoListView();
  }

  Widget _buildMusicInfoListView() {
    return Column(
      children: [
        Row(
          children: [
            VersionDropdownMenu(filterModel: filter),
            LevelDropdownMenu(filterModel: filter)
          ],
        ),
        MusicListContent(musicDataList: _doFilter(musicDataList, filter))
      ],
    );
  }

  void _init() {
    // 读取曲目数据文件，反序列化
    Future<String> musicDataSource = rootBundle.loadString('assets/data/music_data_transformed.json');
    musicDataSource.then((dataSource) {
      List<dynamic> musicDataObjectList = jsonDecode(dataSource) as List<dynamic>;
      var parsedMusicDataList = musicDataObjectList.map((e) {
        return MusicData.fromJson(e as Map<String, dynamic>);
      }).toList();

      parsedMusicDataList.sort((a, b) {
        return a.title.compareTo(b.title);
      });

      setState(() {
        musicDataList = parsedMusicDataList;
      });
    });
  }

  List<MusicData> _doFilter(List<MusicData> musicDataList, FilterModel filter) {
    List<MusicData> ret = [];
    musicDataList.forEach((data) {
      bool filterResult = true;

      if (filter.versionId != null) {
        int songVersionId = data.songId ~/ 1000;
        if (songVersionId != filter.versionId) {
          filterResult = false;
        }
      }
      if (!filterResult) {
        return;
      }

      if (filter.level != null) {
        if (data.spbLevel != filter.level
            && data.spnLevel != filter.level
            && data.sphLevel != filter.level
            && data.spaLevel != filter.level
            && data.splLevel != filter.level
            && data.dpbLevel != filter.level
            && data.dpnLevel != filter.level
            && data.dphLevel != filter.level
            && data.dpaLevel != filter.level
            && data.dplLevel != filter.level) {
          filterResult = false;
        }
      }

      if (filterResult) {
        ret.add(data);
      }
    });

    return ret;
  }
}

